package com.bfsuol.bfsuolframework.security.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bfsuol.bfsuolframework.core.controller.SpringControllerSupport;
import com.bfsuol.bfsuolframework.core.controller.json.MessageJson;
import com.bfsuol.bfsuolframework.core.controller.support.Pagination;
import com.bfsuol.bfsuolframework.core.controller.support.QueryParam;
import com.bfsuol.bfsuolframework.core.spring.QueryParameter;
import com.bfsuol.bfsuolframework.security.annotations.SecCreate;
import com.bfsuol.bfsuolframework.security.annotations.SecEdit;
import com.bfsuol.bfsuolframework.security.annotations.SecList;
import com.bfsuol.bfsuolframework.security.annotations.SecPrivilege;
import com.bfsuol.bfsuolframework.security.dao.Privilege;
import com.bfsuol.bfsuolframework.security.dao.RolePrivilege;
import com.bfsuol.bfsuolframework.security.service._PrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by andy on 2020/10/19.
 */


@Controller
@RequestMapping("/components/security/privilege")
public class _PrivilegeController extends SpringControllerSupport {

    @Autowired
    private _PrivilegeService privilegeService;

    @SecPrivilege(title = "权限设置")
    @RequestMapping("/index")
    public ModelAndView index(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("index"));
        return modelAndView;
    }

    @SecList
    @RequestMapping("/list")
    public ModelAndView list(
            @QueryParameter("query") QueryParam query,
            @ModelAttribute("page") Pagination page){

        com.baomidou.mybatisplus.extension.plugins.pagination.Page page1 = new Page(page.getDefaultCurrentPage(),page.getPageSize());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page privilegePages = privilegeService.page(page1,new QueryWrapper<>());
        page.setItems(privilegePages.getRecords());
        page.setTotalRecord((int) privilegePages.getTotal());

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("list"));
        return modelAndView;
    }

    @RequestMapping("/input")
    public ModelAndView create(@ModelAttribute RolePrivilege rolePrivilege, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("input"));
        return modelAndView;
    }

    @RequestMapping("/input/{id}")
    public ModelAndView edit(@PathVariable("id") Long id, Model model){
        Privilege privilege = privilegeService.getById(id);
        model.addAttribute("privilege",privilege);

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(getViewNamedPath("input"));
        return modelAndView;
    }

    @SecCreate
    @SecEdit
    @RequestMapping("/save")
    public @ResponseBody
    MessageJson save(@ModelAttribute Privilege privilege){
        if(privilege.isNew()){
            privilegeService.save(privilege);
        }else{
            privilegeService.updateById(privilege);
        }
        return MessageJson.information("保存成功");
    }

}
